﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CoyoEden;
using System.Collections.Generic;
using CoyoEden.Models;
using Vivasky.Core;

namespace Controls
{
	/// <summary>
	/// Post Title List
	/// </summary>
	public class PostTitleList:Control
	{

		#region member variables
		private string _CssClass;
		/// <summary>
		/// Css class
		/// </summary>
		public string CssClass {
			get {
				return string.IsNullOrEmpty(_CssClass) ? "" : _CssClass ;
			}
			set { _CssClass = value; }
		}
		private List<Post> _Posts;
		/// <summary>
		/// Posts to be showed
		/// </summary>
		public List<Post> Posts {
			get {
				return _Posts;
			}
			set {
				if (_Posts != value) {
					_Posts = value;
					_Html = null;
				}
			}
		}
		private bool _ShowDate;
		/// <summary>
		/// Show post's date?
		/// </summary>
		public bool ShowDate {
			get {
				return _ShowDate;
			}
			set {
				if (_ShowDate != value) {
					_ShowDate = value;
					_Html = null;
				}
			}
		}
		private string _DateFormat;
		public string DateFormat {
			get {
				return string.IsNullOrEmpty(_DateFormat) ? "yyyy/MM/dd" : _DateFormat; ;
			}
			set {
				if (_DateFormat != value) {
					_DateFormat = value;
					_Html = null;
				}
			}
		}
		private bool _ForceRefresh;
		/// <summary>
		/// refresh control's content everytime when the control was request.
		/// </summary>
		public bool ForceRefresh{
			get {
				return _ForceRefresh;
			}
			set {
				_ForceRefresh = value;
			}
		}
		/// <summary>
		/// Title's length
		/// </summary>
		public int TitleSize { get; set; }

		private static object _SyncRoot = new object();
		private static string _Html;
		/// <summary>
		/// Caches the rendered HTML in the private field and first
		/// updates it when a post has been saved (new or updated).
		/// </summary>
		private string Html
		{
			get
			{
				if (_Html == null || ForceRefresh == true)
				{
					lock (_SyncRoot)
					{
						if (_Html == null||ForceRefresh==true)
						{
							HtmlGenericControl ul = this.BuildList();
							System.IO.StringWriter sw = new System.IO.StringWriter();
							ul.RenderControl(new HtmlTextWriter(sw));
							_Html = sw.ToString();
						}
					}
				}

				return _Html;
			}
		}
		#endregion

		#region helper methods
		private HtmlGenericControl BuildList() {
			HtmlGenericControl ul = new HtmlGenericControl("ul");
			ul.ID = string.IsNullOrEmpty(this.ID) ? "posttitlelist" : this.ID;
			ul.Attributes["class"] = this.CssClass;

			if (Posts == null || Posts.Count == 0) {
				ul.InnerHtml = string.Format("<li>{0}</li>","没有任何新闻/文章可以显示");
				return ul;
			}
			Posts.ForEach(post => {
				TitleSize = (TitleSize == 0) ? 30 : TitleSize;
				HtmlAnchor title=new HtmlAnchor();
				title.HRef=post.RelativeLink;
				title.InnerHtml=post.Title.TailStr(TitleSize,"...");
				title.Title="";

				HtmlGenericControl li = new HtmlGenericControl("li");

				if (ShowDate)
				{
					HtmlGenericControl tempDiv = null;

					//title link wrapper 
					HtmlGenericControl linkwrapper = new HtmlGenericControl("div");
					linkwrapper.Attributes["class"] = "linkwrapper";
					linkwrapper.Controls.Add(title);
					//title part
					tempDiv = new HtmlGenericControl("div");
					tempDiv.Attributes["class"] = "title left";
					tempDiv.Controls.Add(linkwrapper);
					li.Controls.Add(tempDiv);

					//date part
					tempDiv = new HtmlGenericControl("div");
					tempDiv.Attributes["class"] = "date right";
					tempDiv.InnerHtml = string.Format("<span class=\"{0}\">[{1}]</span>", "datespan", post.DateModified.ToString(DateFormat));
					li.Controls.Add(tempDiv);

					//clearDiv
					li.Controls.Add(new LiteralControl("<div class=\"clear\"></div>"));
				}
				else {
					li.Controls.Add(title);
				}

				//add to ul
				ul.Controls.Add(li);
			});
			return ul;
		}
		#endregion

		/// <summary>
		/// Render this control
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderControl(HtmlTextWriter writer)
		{
			writer.Write(Html);
			writer.Write(Environment.NewLine);
		}

	}
}
